<?php 
include TMPL_PATH.'public/header.phtml';
?>
<div class="subnav">
    <div class="content-menu ib-a blue line-x">
        <a href='<?php echo U('promotion/wholesale_list');?>'><em>批发管理</em></a><span>|</span><a href='javascript:;' class="on" ><em>编辑批发方案</em></a>
    </div>
</div>
<div class="pad_10">
<div class="common-form">
<form name="myform" action="__SELF__" method="post" id="myform">
<table width="100%" class="table_form contentWrap">
<tr>
	<th width="150">批发商品名称：</th> 
	<td><?php echo $wholesale['product_name'];?>
    <input type="hidden" value="<?php echo $wholesale['product_name'];?>" id="product_name" name="product_name">
    <input type="hidden" value="<?php echo $wholesale['product_id'];?>" name="product_id">
    </td>
</tr>
<tr>
	<th>适用会员等级：</th> 
	<td>
	<?php $rank_ids = $wholesale['rank_ids'] ? explode(',', $wholesale['rank_ids']) : null;?>
	<input type="checkbox" value="0" id="rank_id[]" <?php if(empty($rank_ids))echo 'checked';?> name="rank_id[]"> 所有人 
	<?php 
		if(isset($rankList) && $rankList):
			foreach ($rankList as $rank):
	?>
	<input type="checkbox" value="<?php echo $rank['id']?>" <?php if(!is_null($rank_ids) && in_array($rank['id'], $rank_ids))echo 'checked';?> id="rank_id[]" name="rank_id[]"> <?php echo $rank['name']?> 
	<?php 
			endforeach;
		endif;
	?>
	</td>
</tr>
<tr>
	<th>是否启用：</th> 
	<td><label>
	        <input type="radio" <?php if($wholesale['display']==1)echo 'checked';?> value="1" name="display">
	        是</label>
	      <label>
	        <input type="radio" value="0" <?php if($wholesale['display']==0)echo 'checked';?> name="display">
	        否</label>
	 </td>
</tr>
</table>
 <div class="bk15"></div>
<hr />
 <div class="bk15"></div>
<div id="price-div">
<table width="100%" class="table_form contentWrap">
    <tbody>
    <?php 
    if($wholesale['prices']):
    	$prices = unserialize($wholesale['prices']);
    	foreach ($prices as $i => $list):
    		if($i==0):
    ?>
    
    <tr>
    <td width="10%">&nbsp;</td>
    <td> 数量 <input type="text" class="input-text" value="<?php echo $list['qty']?>" name="qty[]"> </td>
    <td> 价格 <input type="text" class="input-text" value="<?php echo $list['price']?>" name="price[]"> </td>
    <td>  <input type="button" onclick="addQuantityPrice(this, '0')" value=" + " class="button">  </td>
    <td width="10%">&nbsp;</td>
  </tr>
  <?php else:?>
  <tr>
    <td width="10%">&nbsp;</td>
    <td> 数量 <input type="text" class="input-text" value="<?php echo $list['qty']?>" name="qty[]"> </td>
    <td> 价格 <input type="text" class="input-text" value="<?php echo $list['price']?>" name="price[]"> </td>
    <td>  <input type="button" class="button" value=" - " onclick="dropQuantityPrice(this)" />  </td>
    <td width="10%">&nbsp;</td>
  </tr>
  <?php	
  			endif;
			endforeach;
 		else:
  ?>
    <tr>
    <td width="10%">&nbsp;</td>
    <td> 数量 <input type="text" class="input-text" value="0" name="qty[]"> </td>
    <td> 价格 <input type="text" class="input-text" value="0" name="price[]"> </td>
    <td>  <input type="button" onclick="addQuantityPrice(this, '0')" value=" + " class="button">  </td>
    <td width="10%">&nbsp;</td>
  </tr>
  <?php endif;?>
  </tbody></table>
</div>
    <div class="bk15"></div>
    <input name="dosubmit" type="submit" value="提交" class="button" id="dosubmit" />
</form>
</div>
</div>
</body>
</html>
<script type="text/javascript">
function searchGoods()
{
	var filter = new Object;
  filter.keyword  = document.forms['myform'].elements['keyword'].value;
	filter.cat_id  = document.forms['myform'].elements['cat_id'].value;
	filter.brand_id  = document.forms['myform'].elements['brand_id'].value;
	$.getJSON('<?php echo U('promotion/search_product_ajax');?>', filter, searchGoodsResponse);
}
function searchGoodsResponse(result)
{
  if (result.error == '1' && result.message != '')
  {
    alert(result.message);
		return;
  }

  var sel = document.forms['myform'].elements['product_id'];

  sel.length = 0;

  /* 创建 options */
  var goods = result.content;
  if (goods)
  {
    for (i = 0; i < goods.length; i++)
    {
      var opt = document.createElement("OPTION");
      opt.value = goods[i].id;
      opt.text  = goods[i].name;
      sel.options.add(opt);
    }
  }

  return;
}

/**
 * @param object buttonObj
 * @param int    tableIndex
 */
function addQuantityPrice(buttonObj, tableIndex)
{
  var tableObj = buttonObj.parentNode.parentNode.parentNode.parentNode;
  var newRow = tableObj.insertRow(-1);
  var cell1 = newRow.insertCell(-1);
  var cell2 = newRow.insertCell(-1);
  var cell3 = newRow.insertCell(-1);
  var cell4 = newRow.insertCell(-1);
  var cell5 = newRow.insertCell(-1);
  
  cell1.innerHTML = '&nbsp;';
  cell2.innerHTML = ' 数量 <input class="input-text" name="qty[]" type="text" value="" /> ';
  cell3.innerHTML = ' 价格 <input class="input-text" name="price[]" type="text" value="" /> ';
  cell4.innerHTML = ' <input type="button" class="button" value=" - " onclick="dropQuantityPrice(this)" />';
  cell5.innerHTML = '&nbsp;';
  
}

/**
 * @param object buttonObj
 */
function dropQuantityPrice(buttonObj)
{
  var trObj = buttonObj.parentNode.parentNode;
  var tableObj = trObj.parentNode.parentNode;
  tableObj.deleteRow(trObj.rowIndex);
}
</script>